package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.CustomerBean;
import com.project.bean.ProductBean;
import com.project.bean.SellCarBean;
import com.project.mapper.ICustomerMapper;
import com.project.mapper.IProductMapper;
import com.project.mapper.ISellCarMapper;
import com.project.service.ISellCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class SellCarServiceImp implements ISellCarService {
    @Autowired
    private ICustomerMapper customerMapper;
    @Autowired
    private ISellCarMapper sellCarMapper;
    @Autowired
    private IProductMapper productMapper;
    @Override
    public List<CustomerBean> findAll() {
        return customerMapper.selectList(null);
    }

    @Override
    public List<SellCarBean> findBySellerId(Integer sellerId) {
        QueryWrapper<SellCarBean> qw = new QueryWrapper<>();
        qw.eq("fk_userId",sellerId);
        List<SellCarBean> sellCarBeans = sellCarMapper.selectList(qw);

        List<SellCarBean> newSellCarBeans = new ArrayList<>();
        for (SellCarBean sellCarBean : sellCarBeans){
            sellCarBean.setProductBeanInSellCar(productMapper.selectById(sellCarBean.getProductId()));
            newSellCarBeans.add(sellCarBean);
        }

        return newSellCarBeans;
    }

    @Override
    public IPage<ProductBean> findByItem(Integer pageNO, String productType, String productName) {
        QueryWrapper<ProductBean> qw = new QueryWrapper<>();
        qw.ne("p_num", 0);//不等于0
        qw.ne("p_outPrice", 0);//不等于0
        if (productType != null && !productType.equals("") ){
            qw.like("p_type",productType);
        }
        if (productName!= null && !productName.equals("")  ){
            qw.like("p_name",productName);
        }
        IPage<ProductBean> iPage=productMapper.selectPage(new Page<>(pageNO,3),qw);
        List<ProductBean> products=iPage.getRecords();

        List<ProductBean> productBeanList = new ArrayList<>();
        for (ProductBean productBean : products){
            if (productBean.getNum()>0){
                productBeanList.add(productBean);
            }
        }
        iPage.setRecords(productBeanList);
        return iPage;
    }


    @Override
    public void addProductInSellCar(Integer productId) {
        // 添加销售商品---在productservice写了
    }

    @Override
    public void delProductInSellCar(Integer sellCarId) {
        sellCarMapper.deleteById(sellCarId);
    }

    @Override
    public void updateProductNumInSellCar(Integer sellCarId, Integer newNum) {
        SellCarBean sellCarBean = sellCarMapper.selectById(sellCarId);
        sellCarBean.setNum(newNum);
        sellCarMapper.updateById(sellCarBean);

        String  productId = sellCarBean.getProductId();
        QueryWrapper<ProductBean> query = new QueryWrapper<>();
        query.eq("pk_productId", productId);
        ProductBean productBean = productMapper.selectOne(query);
        productBean.setNum(productBean.getNum()-newNum);
        productMapper.updateById(productBean);
    }
}
